import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
// 前台根视图
// import Index from '@/layouts/index.vue'

// 导入所有router
const metaRouters: any = import.meta.glob('./modules/*.ts', { eager: true, import: 'default' })
// 处理路由
export const routerArray: RouteRecordRaw[] = Object.keys(metaRouters).map((key) => metaRouters[key] || {})

export const routes: RouteRecordRaw[] = [
  // {
  //   path: '/',
  //   name: 'index',
  //   component: Index,
  //   children: [
  //     {
  //       path: 'index',
  //       name: 'IndexBooks',
  //       component: () => import('@/views/web/books/index.vue'),
  //     },
  //   ],
  // },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
    },
  },
  ...routerArray,
  {
    path: '/:pathMatch(.*)*',
    name: 'notFound',
    redirect: { name: '404' },
  },
]
console.log('routes', routes)

const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_BASE_URL as string),
  routes,
})

// 重置路由
export function resetRouter(): void {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export default router
